package com.kgc.crowd.distributioncrowddatabaseprovider.service.impl;

import com.kgc.crowd.distributioncrowdcommon.pojo.Member;
import com.kgc.crowd.distributioncrowdcommon.pojo.MemberExample;
import com.kgc.crowd.distributioncrowdcommon.pojo.MemberLaunchInfoPO;
import com.kgc.crowd.distributioncrowdcommon.pojo.MemberLaunchInfoPOExample;
import com.kgc.crowd.distributioncrowdcommon.util.CrowdUtils;
import com.kgc.crowd.distributioncrowddatabaseprovider.mapper.MemberLaunchInfoPOMapper;
import com.kgc.crowd.distributioncrowddatabaseprovider.mapper.MemberMapper;
import com.kgc.crowd.distributioncrowddatabaseprovider.service.MemberService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Transactional(readOnly = true)
@Service
public class MemberServiceImpl implements MemberService {
    @Resource
    MemberMapper memberMapper;
    @Resource
    MemberLaunchInfoPOMapper memberLaunchInfoPOMapper;
    @Override
    public Integer getLoignAcctCount(String loginAcct) {
        // 1.创建MemberPOExample
        MemberExample example = new MemberExample();

        // 2.封装查询条件
        example.createCriteria().andLoginacctEqualTo(loginAcct);

        // 3.执行查询
        return memberMapper.countByExample(example);
    }

    @Transactional(readOnly = false,propagation = Propagation.REQUIRED)
    @Override
    public void saveMember(Member member) {
        memberMapper.insert(member);
    }
    @Override
    public Member getMemberByLoginAcct(String loginAcct) {
        // 1.创建MemberPOExample对象
        MemberExample example = new MemberExample();

        // 2.封装查询条件
        example.createCriteria().andLoginacctEqualTo(loginAcct);

        // 3.执行QBC查询
        List<Member> memberList = memberMapper.selectByExample(example);

        // 4.判断查询结果集合的有效性
        if(CrowdUtils.collectionEffectiveCheck(memberList)) {

            // 5.如果查询结果集合有效，则返回第一个元素
            return memberList.get(0);
        }

        // 6.如果集合无效则返回null
        return null;
    }

    @Override
    public MemberLaunchInfoPO getMemberLaunchInfoByMember(Integer memberId) {
        MemberLaunchInfoPOExample example=new MemberLaunchInfoPOExample();
        example.createCriteria().andMemberidEqualTo(memberId);
        List<MemberLaunchInfoPO> memberLaunchInfoPOS = memberLaunchInfoPOMapper.selectByExample(example);
        if(CrowdUtils.collectionEffectiveCheck(memberLaunchInfoPOS)){
            return memberLaunchInfoPOS.get(0);
        }
        return null;
    }
}
